package com.success.application.ground.holder;


import android.app.AlertDialog;
import android.app.Application;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;

import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.TextHttpResponseHandler;
import com.success.application.YuanbaoApplication;
import com.success.application.common.result.Result;
import com.success.application.ground.bean.Comment;
import com.success.application.ground.bean.Thumb;
import com.success.application.ground.cust_view.CusListView;
import com.success.application.ground.cust_view.HorizontalListView;
import com.success.application.ground.cust_view.HorizontalListViewAdapter;
import com.success.application.ground.cust_view.LikesView;
import com.success.application.ground.cust_view.NineGridTestLayout;
import com.success.application.myapplication.R;

import android.os.Handler;
import android.support.design.widget.CoordinatorLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.success.application.api.YuanbaoApi;
import com.success.application.base.BaseHolder;
import com.success.application.common.param.Params;
import com.success.application.ground.bean.Friend;
import com.success.application.user.AccountHelper;
import com.success.application.util.BottomSheetBar;
import com.success.application.util.CommentBar;
import com.success.application.util.DialogHelper;
import com.success.application.widget.GsonUtils;
import com.success.application.widget.SimplexToast;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;

import static com.success.application.myapplication.R.id.imageButton;

/**
 * Created by ChrisWong on 2017/1/17.
 */

public class FriendHolder extends BaseHolder<Friend> {
    private Friend mfriend;
    //朋友圈內容ID
    String id;
    //下方评论条
    private CommentBar mDelegation;
    //评论框
    EditText mViewInput;
    //弹框
    private Dialog dialog;
    protected Result<Map> result;
    private int dzType;
    private String dzId;//点赞id

    @Bind(R.id.iv_consult_face)
    ImageView iv_consult_face;
    @Bind(R.id.tv_username)
    TextView tv_username;
    @Bind(R.id.tv_content)
    TextView tv_content;
    @Bind(R.id.tv_activity_time)
    TextView tv_activity_time;//时间
    @Bind(imageButton)//点赞和评论的按钮
    public ImageButton im_button;
    @Bind(R.id.layout_coordinator)
    public CoordinatorLayout mCoordinatorLayout;
    @Bind(R.id.cus_listView)
    public CusListView cus_listView;
    @Bind( R.id.horizon_listview)
    public HorizontalListView horizon_listview;
    @Bind(R.id.layout_nine_grid)
    public NineGridTestLayout layout;
    public TextView tv_zan;
    private PopupWindow mMorePopupWindow;
    private int mShowMorePopupWindowWidth;
    private int mShowMorePopupWindowHeight;
    HorizontalListViewAdapter hListViewAdapter;
    public FriendHolder(View itemView) {
        super(itemView);
    }



    @Override
    public void bindItem(Friend friend) {
        mfriend = friend;
        id = mfriend.getId();
        Glide.with(iv_consult_face.getContext()).load("http://47.92.92.215:8087"+mfriend.getUser_tx()).into(iv_consult_face);
        tv_username.setText(mfriend.getUser_nc());
        tv_activity_time.setText(mfriend.getPyq_fbsj());
        //活动备注==活动详情 html的文本转换
        tv_content.setText(mfriend.getPyq_zt());
        List comments = mfriend.getPllist();
        for(int i = 0 ; i < comments.size() ; i++){
//            Comment comment = (Comment) thumbs.get(i);
//            mtv_username_comment.setText(comment.getPl_zt());
            //显示评论人
//            LayoutInflater li =  (LayoutInflater).getSystemService
//                    (Context.LAYOUT_INFLATER_SERVICE);
//            View content = LayoutInflater.inflate(R.layout.item_popip, null, false);
//            View content = LayoutInflater
        }
        //设置文本信息
//        im_button.setOnClickListener(new View.OnClickListener() {
//        @Override
//        public void onClick(View v) {
//            //點擊
//            if (mMorePopupWindow == null) {
//                LayoutInflater li =  (LayoutInflater)v.getContext().getSystemService
//                        (Context.LAYOUT_INFLATER_SERVICE);
//                View content = li.inflate(R.layout.item_popip, null, false);
//                mMorePopupWindow = new PopupWindow(content, ViewGroup.LayoutParams.WRAP_CONTENT,
//                        ViewGroup.LayoutParams.WRAP_CONTENT);
//                mMorePopupWindow.setBackgroundDrawable(new BitmapDrawable());
//                mMorePopupWindow.setOutsideTouchable(true);
//                mMorePopupWindow.setTouchable(true);
//                mMorePopupWindow.setFocusable(true);
//                content.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
//                mShowMorePopupWindowWidth = content.getMeasuredWidth();
//                mShowMorePopupWindowHeight = content.getMeasuredHeight();
//                View parent = mMorePopupWindow.getContentView();
//                ImageView like = (ImageView) parent.findViewById(R.id.btn_zan);
//                tv_zan = (TextView) parent.findViewById(R.id.tv_zan);
//                ImageView comment = (ImageView) parent.findViewById(R.id.btn_comment);
//                List thumbs = mfriend.getDzlist();
//                if(thumbs!=null){
//                    for(int i = 0 ; i < thumbs.size() ; i++){
//                        //显示点赞人
//                        Thumb thumb = (Thumb) thumbs.get(i);
//                        if(thumb.getUser_id().equals(AccountHelper.getUserId())){
//                            dzType = 0;
//                        }else{
//                            dzType = 1;
//                            dzId = thumb.getId();
//                        }
//                    }
//                    if(thumbs.size()==0){
//                        dzType = 0;
//                    }
//
//                }else{
//                    dzType = 0;
//                }
//                //未点赞
//                if(dzType==0){
//                    tv_zan.setText("赞");
//                }else{
//                    tv_zan.setText("取消");
//                }
//               //点赞的监听器
//                like.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        //进行点赞
//                        mMorePopupWindow.dismiss();
//                        if(AccountHelper.getUserId()!=null&&dzType==0){
//                            Params params = new Params();
//                            params.put("model.user_id",(int) Double.parseDouble(AccountHelper.getUserId()));
//                            params.put("model.pyqId",(int) Double.parseDouble(mfriend.getId()));
//                            YuanbaoApi.pubAddDz(params, new DianZanFragmentHandler());
//                        }else{
//                            Params params = new Params();
//                            params.put("id",Integer.valueOf(dzId));
//                            YuanbaoApi.qxDz(params, new QuXiaoFragmentHandler());
//                        }
//
//                    }
//                });
//
//                // 评论的监听器
//                comment.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        //进行评论
//                        mMorePopupWindow.dismiss();
//                        mDelegation = CommentBar.delegation(v.getContext(), mCoordinatorLayout);
//                        mDelegation.getBottomSheet().setCommitListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//                                String content = mDelegation.getBottomSheet().getCommentText().replaceAll("[\\s\\n]+", " ");
//                                if (TextUtils.isEmpty(content)) {
//                                    SimplexToast.show(v.getContext(),"请输入文字");
//                                    return;
//                                }
//                                dialog = DialogHelper.getProgressDialog(v.getContext(), "正在发表评论...");
//                                dialog.show();
//                                //封装参数
//
//                                if(AccountHelper.getUserId()!=null){
//                                    Params params = new Params();
//                                    params.put("model.pl_plrId",(int) Double.parseDouble(AccountHelper.getUserId()));
//                                    params.put("model.pl_bplId",(int) Double.parseDouble(mfriend.getId()));
//                                    params.put("model.pl_lx",0);
//                                    params.put("model.pl_zt",content);
//                                    YuanbaoApi.addPl(params, new IndexFragmentHandler());
//                                }
//
//                            }
//                        });
//                    }
//                });
//            }else{
//                mMorePopupWindow.dismiss();
//            }
//            if (mMorePopupWindow.isShowing()) {
//                mMorePopupWindow.dismiss();
//            } else {
//                int heightMoreBtnView = v.getHeight();
//                mMorePopupWindow.showAsDropDown(v, -mShowMorePopupWindowWidth,
//                        -(mShowMorePopupWindowHeight + heightMoreBtnView) / 2);
//            }
//        }
//    });
    }

    //@Override
//    public void button_click(View v) {
//        Toast.makeText(v.getContext(),
//                "长按事件触发！", Toast.LENGTH_SHORT)
//                .show();
//        //设置文本信息
//        Log.d("111","111");
//    }
    private void handleKeyDel() {


    }
    @Override
    public boolean onLongClick(View v) {
//        Toast.makeText(v.getContext(),
//                mConsult.getNc()+ "长按事件触发！", Toast.LENGTH_SHORT)
//                .show();
        return true;
    }
//    // 自定义加载监听器
//    public class IndexFragmentHandler extends TextHttpResponseHandler {
//
//        @Override
//        public void onSuccess(int statusCode, Header[] headers, String responseString) {
//            try {
//                Type type = new TypeToken<Result<Map>>() {
//                }.getType();
//                result = GsonUtils.fromJson(responseString, type);
//                if (result != null && Result.RESULT_SUCCESS.equals("success")) {
//                    dialog.dismiss();
//                    mDelegation.getBottomSheet().dismiss();
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//                onFailure(statusCode, headers, responseString, e);
//            }
//        }
//
//        @Override
//        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//        }
//    }
//
//    public class DianZanFragmentHandler extends TextHttpResponseHandler {
//
//        @Override
//        public void onSuccess(int statusCode, Header[] headers, String responseString) {
//            try {
//                tv_zan.setText("取消");
//                dzType = 1;
//                //添加点赞的点赞人
//                String[] titles = new String[64];
//                String[] ids = new String[64];
//                titles[0] =AccountHelper.getUser().getUser_nc();
//                ids[0] = AccountHelper.getUser().getUser_tx();
//
//                hListViewAdapter = new HorizontalListViewAdapter(YuanbaoApplication.getContextObject(),titles,ids);
//                horizon_listview.setAdapter(hListViewAdapter);
////                Type type = new TypeToken<Result<Map>>() {
////                }.getType();
////                result = GsonUtils.fromJson(responseString, type);
////                if (result != null && Result.RESULT_SUCCESS.equals("success")) {
////                    dialog.dismiss();
////                    mDelegation.getBottomSheet().dismiss();
////                }
//            } catch (Exception e) {
//                e.printStackTrace();
//                onFailure(statusCode, headers, responseString, e);
//            }
//        }
//
//        @Override
//        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//        }
//    }
//    //
//    public class QuXiaoFragmentHandler extends TextHttpResponseHandler {
//
//        @Override
//        public void onSuccess(int statusCode, Header[] headers, String responseString) {
//            try {
//                dzType = 0;
//                tv_zan.setText("赞");
//                mfriend.getDzlist();
//                //添加点赞的点赞人
//                String[] titles = new String[64];
//                String[] ids = new String[64];
//                titles[0] =AccountHelper.getUser().getUser_nc();
//                ids[0] = AccountHelper.getUser().getUser_tx();
//                hListViewAdapter = new HorizontalListViewAdapter(YuanbaoApplication.getContextObject(),titles,ids);
//                horizon_listview.setAdapter(hListViewAdapter);
////                Type type = new TypeToken<Result<Map>>() {
////                }.getType();
////                result = GsonUtils.fromJson(responseString, type);
////                if (result != null && Result.RESULT_SUCCESS.equals("success")) {
////                    dialog.dismiss();
////                    mDelegation.getBottomSheet().dismiss();
////                }
//            } catch (Exception e) {
//                e.printStackTrace();
//                onFailure(statusCode, headers, responseString, e);
//            }
//        }
//
//        @Override
//        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//        }
//    }
}
